<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script type="text/javascript">
    var student = {
        //普通属性
        "name": "小明",
        "age": 18,
        "graduated": true,
        //作为数组的属性
        "parent_name": ["张三", "李四", 20, false],
        //对象属性
        "teacher": {
            "name": "老师1",
            "major": "math",
            "age": 40
        },
        //对象数组属性
        "friends": [
            {"name": "王五", 'age': 30}, {"name": "赵六", "age": 40}
        ]
    };
    //对JSON对象的取值
    console.log(student.name);
    console.log(student.friends);
    //对JSON对象属性动态赋值
    student.school_name = "第一中学";
    console.log(student);
    console.log('================');
    //1.访问学生的前三个属性
    console.log(student.name);
    console.log(student.age);
    console.log(student.graduated);
    //2.遍历 parent_name
    for (var i = 0; i < student.parent_name.length; i++) {
        console.log(student.parent_name[i]);
    }
    //3.访问teacher对象的age
    console.log(student.teacher.age);
    //4.遍历每个friends的 name 属性
    for (var i = 0; i < student.friends.length; i++) {
        console.log(student.friends[i].name);
    }

    //把JSON对象转换成JSON字符串
    var studentObjStr = JSON.stringify(student);
    console.log(studentObjStr);
    //把JSON字符串转换成JSON对象
    var jsonObj = JSON.parse(studentObjStr);
    console.log(jsonObj);
    //打印当前对象的类型
    //string
    console.log(typeof studentObjStr);
    //object
    console.log(typeof jsonObj);
</script>
</body>
</html>